##########################################################################
# This file is part of Hawkengine.
#
# Hawkengine is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Hawkengine is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Hawkengine.  If not, see <http://www.gnu.org/licenses/>.
##########################################################################

include build/functions.mk

##########################################################################
NAME=hawkengine
VERSION=HAWKENGINE_VERSION
CC=gcc
CC_LD=$(CC)
SHELL=sh
BINDIR=bin
RESDIR=data
INCDIR=include
LIBDIR=lib
SRCDIR=src
APPDIR=app
COREDIR=core
MAKEDIR=build
PLATFORMDIR=platform
TARGETDIR=target
TMPDIR=tmp
BINEXT=
LIBLEAD=
LIBEXT=
DEBUG=
MOD=
LMOD=$(call lc,$(MOD))
CMOD=$(call cap,$(MOD))
UMOD=$(call uc,$(MOD))
ifneq ($(MOD),)
HYPHEN_LMOD=-$(LMOD)
HYPHEN_CMOD=-$(CMOD)
HYPHEN_UMOD=-$(UMOD)
UNDER_LMOD=_$(LMOD)
UNDER_CMOD=_$(CMOD)
UNDER_UMOD=_$(UMOD)
endif
INCDIRS=$(INCDIR) $(INCDIR)/$(APPDIR) $(TMPDIR)/$(INCDIR)
DEFAULT_CFLAGS+=-Wall \
	$(DEBUG) \
	$(patsubst %, -D%, $(UMOD))
DEFAULT_CFLAGS+=$(foreach INC, $(INCDIRS), -I$(INC))
CFLAGS+=$(DEFAULT_CFLAGS)
LDFLAGS+=-L$(LIBDIR)
DELETE_FILE=rm -f
DELETE_FOLDER=rm -rf
ifeq ($(DEBUG),)
BUILD_TARGET=release
else
BUILD_TARGET=debug
endif

##########################################################################


##########################################################################
# platform control
PLATFORM=$(shell uname)
CFLAGS+=-D$(call uc,$(PLATFORM))

include $(MAKEDIR)/$(PLATFORMDIR)/*.mk

##########################################################################


##########################################################################
# targets
APPS=$(patsubst $(MAKEDIR)/$(APPDIR)/%.mk,%,$(wildcard $(MAKEDIR)/$(APPDIR)/*.mk))
LIBRARIES=$(patsubst $(MAKEDIR)/$(LIBDIR)/%.mk,%,$(wildcard $(MAKEDIR)/$(LIBDIR)/*.mk))
TARGETS=$(patsubst $(MAKEDIR)/$(TARGETDIR)/%.mk,%,$(wildcard $(MAKEDIR)/$(TARGETDIR)/*.mk))
ALL_APPS_TARGETS=$(foreach TARGET,$(TARGETS),$(foreach APP,$(filter-out test,$(APPS)),$(APP)_$(TARGET)))

all: external_libs common $(ALL_APPS_TARGETS)

include $(MAKEDIR)/$(COREDIR)/*.mk

include $(MAKEDIR)/$(LIBDIR)/*.mk

include $(MAKEDIR)/$(APPDIR)/*.mk

include $(MAKEDIR)/$(TARGETDIR)/*.mk

##########################################################################


##########################################################################
# supporting targets
common: $(HOST_FILE) $(VERSION_FILE)

external_libs: $(LIBRARIES)

define compile_template
$(TMPDIR)/$1/%.o: $(SRCDIR)/$1/%.c
	@mkdir -p $$(@D)
	$(CC) -c -o $$@ $$< $$(CFLAGS) $$($(call uc,$(subst /,_,$1))_CFLAGS)

$(TMPDIR)/$1/%.o: $(SRCDIR)/$1/%.cpp
	@mkdir -p $$(@D)
	$(CC) -c -o $$@ $$< $$(CFLAGS) $$($(call uc,$(subst /,_,$1))_CFLAGS)
endef
_SRC_DIRS+=$(foreach _DATA_SRC_DIR,$(wildcard $(SRCDIR)/data/*),$(patsubst $(SRCDIR)/data/%/, data/%, $(_DATA_SRC_DIR)))
_SRC_DIRS+=$(foreach _LIB_SRC_DIR,$(wildcard $(SRCDIR)/lib/*/),$(patsubst $(SRCDIR)/lib/%/, lib/%, $(_LIB_SRC_DIR)))
_SRC_DIRS+=$(foreach _SRC_DIR,$(wildcard $(SRCDIR)/*/),$(patsubst $(SRCDIR)/%/, %, $(_SRC_DIR)))
$(foreach DIR,$(_SRC_DIRS), $(eval $(call compile_template,$(DIR))))

$(TMPDIR)/%.o: $(SRCDIR)/%.c
	@mkdir -p $(@D)
	$(CC) -c -o $@ $< $(CFLAGS)

$(BINDIR)/%:
	mkdir -p $@

##########################################################################

